<?php

use Plugins\WebSite\Http\Admin\Controller\PageController;
use Plugins\WebSite\Http\PageInfo;
use Sc\Util\HtmlStructure\Form\FormItem;
use Sc\Util\HtmlStructure\Html\Html;
use Sc\Util\HtmlStructure\Html\Js\JsFunc;
use Sc\Util\HtmlStructure\Html\Js\VueComponents\Temporary;
use Sc\Util\HtmlStructure\Table;

/** @var PageInfo $pageInfo */

Html::create("页面设置");

$table = Table::create(sc_route([PageController::class, 'pageList']), 'pi')->addColumns(
    Table\Column::index(),
    Table\Column::normal('标题', 'title'),
    Table\Column::normal('页面', 'page'),
    Table\Column::normal('状态', 'enable')->showMapping([
        1 => '启用',
        0 => '禁用'
    ]),
    Table\Column::normal('是首页', 'isIndex')->showMapping([
        1 => '是',
        0 => '否'
    ]),
);
$table->setPagination(false);

$temporary = Temporary::create("pagec")->setContent(
    Sc\Util\HtmlStructure\Form::create("page")->config(['label-width' => '80px'])->addFormItems(
        FormItem::hidden('id', '标题'),
        FormItem::text('title', '标题'),
        FormItem::text('page', '页面')->col(12)->setVAttrs([':disabled' => 'page.id']),
        FormItem::customize("index 为首页")->col(12),
        FormItem::radio('enable', '状态')->options([
            1 => '启用',
            0 => '禁用'
        ])->default(1),
        FormItem::submit()->successClose('current')->submitUrl(
            sc_route([PageController::class, 'save']),
        )->success("VueApp.piGetData()")
    )->setSubmitHandle("delete this.page.id")->render()
);

$table->setHeaderEvent("@primary.Refresh.刷新数据", "this.piGetData()");
$table->setHeaderEvent("@primary.Plus.添加页面", function () use ($temporary){
    return Table\EventHandler::window("添加页面")->setConfig(['width' => '600px'])
        ->setComponent($temporary);
});
$table->setRowEvent("@primary.修改页面", function () use ($temporary){
    return Table\EventHandler::window("修改页面")->setConfig(['width' => '600px'])
        ->setRowData([
            'id' => 1,
            'title' => '@row.title',
            'page' => '@row.page',
            'enable' => '@row.enable'
        ])
        ->setComponent($temporary);
});
$table->setRowGroupEvent("页面操作", function () use ($table){

    $table->setRowEvent("@primary.设置页面代码", function () {
        return Table\EventHandler::post(sc_route([PageController::class, 'saveCode']), [
            'page' => '@row.page',
            'code' => '@childrenWin.editor.getValue()',
        ])->mountIframe(Table\EventHandler::window("设置【{title}】代码")->setConfig(['width' => '1200px'])
            ->setConfig(['btn' => ['保存代码']])
            ->setUrl(sc_route([PageController::class, 'code']), ['page' => '@page']));
    });

    $table->setRowEvent("@primary.编辑页面", function () {
        return Table\EventHandler::post(sc_route([PageController::class, 'saveCode']), [
            'page' => '@row.page',
            'code' => '@childrenWin.editor.getHtml()',
        ])->mountIframe(Table\EventHandler::window("设置【{title}】代码")->setConfig(['width' => '1200px'])
            ->setConfig(['btn' => ['保存页面'], 'parent' => '1', 'success' => JsFunc::arrow(['layero', 'index'])->code('parent.layer.full(index);')])
            ->setUrl(sc_route([PageController::class, 'editPage']), ['page' => '@page']))
            ->fail('console.log(childrenWin.editor.getHtml())');
    });
    $table->setRowEvent("@primary.编辑页面(安全模式)", function () {
        return Table\EventHandler::post(sc_route([PageController::class, 'saveCode']), [
            'page' => '@row.page',
            'code' => '@childrenWin.editor.getHtml()',
        ])->mountIframe(Table\EventHandler::window("设置【{title}】代码")->setConfig(['width' => '1200px'])
            ->setConfig(['btn' => ['保存页面'], 'parent' => '1', 'success' => JsFunc::arrow(['layero', 'index'])->code('parent.layer.full(index);')])
            ->setUrl(sc_route([PageController::class, 'editPage']), ['page' => '@page', 'safe' => 1]))
            ->fail('console.log(childrenWin.editor.getHtml())');
    });

});

return $table->render();